/***************************************************
> Copyright (C) 2024 ==KINGYI== All rights reserved.
> File Name:type.c
> Author:xxx
> Mail: 
> Created Time: 2024年12月04日 星期三 16时08分15秒
***************************************************/

#include "cJSON.h"
#include "style.h"

int cjson_type(char *str){
	//将标准json格式字符串转换为json对象
	cJSON *root = cJSON_Parse(str);
	
	cJSON *type = cJSON_GetObjectItem(root,"type");
	
	//获取字符串类型的值
	char *type_1 = cJSON_GetStringValue(type);
	printf("%s\n",type_1);
	if(strcmp(type_1,"gly_login") == 0){			//管理员登陆
		return 1;
	}else if(strcmp(type_1,"staff_login") == 0){	//员工登陆
		return 2;
	}else if(strcmp(type_1,"kin_login") == 0){		//家属登陆
		return 3;
	}else if(strcmp(type_1,"kin_register") == 0){	//家属注册
		return 4;
	}else if(strcmp(type_1,"staff_register") == 0){	//员工注册
		return 5;
	}else if(strcmp(type_1,"delete_nurse") == 0){	//删除员工
		return 6;
	}else if(strcmp(type_1,"delete_kin") == 0){		//删除家属
		return 7;
	}else if(strcmp(type_1,"staff_change_passwd") == 0){//员工修改密码
		return 8;
	}else if(strcmp(type_1,"kin_change_passwd") == 0){	//家属修改密码
		return 9;
	}else if(strcmp(type_1,"staff_leave") == 0){	//员工请假
		return 10;
	}else if(strcmp(type_1,"write_files") == 0){	//老人档案写入
		return 11;
	}else if(strcmp(type_1,"cat_leave") == 0){	//查看所有假条
		return 12;
	}else if(strcmp(type_1,"gly_examine") == 0){	//审批假条
		return 13;
	}
	return 0;
}

//判断管理员是否登陆成功
int gly_login(int client_socket,char *str){
	//将标准json格式字符串转换为json对象
	cJSON *root = cJSON_Parse(str);
	
	cJSON *username = cJSON_GetObjectItem(root,"username");
	cJSON *passwd = cJSON_GetObjectItem(root,"passwd");
	
	char *username_1 = cJSON_GetStringValue(username);
	char *passwd_1 = cJSON_GetStringValue(passwd);
	
	if(strcmp(username_1,"gly") == 0 && strcmp(passwd_1,"123456") == 0){
		write(client_socket,"success",7);
		return 1;
	}else{
		write(client_socket,"fail",4);
		return 0;
	}
}

//判断员工、家属是否登陆成功
int staff_kin_login(int client_socket,MYSQL *mysql,char *str,int num){
	//将标准json格式字符串转换为json对象
	cJSON *root = cJSON_Parse(str);
	
	cJSON *username = cJSON_GetObjectItem(root,"username");
	cJSON *passwd = cJSON_GetObjectItem(root,"passwd");
	
	char *username_1 = cJSON_GetStringValue(username);
	char *passwd_1 = cJSON_GetStringValue(passwd);
	char src[1024] = {0};
	if(num == 2){
		//判断员工是否登陆成功
		sprintf(src,"select * from nurse where nursename = %s and passwd = %s",username_1,passwd_1);
	}else{
		//判断家属是否登陆成功
		sprintf(src,"select * from family_members where username = %s and passwd = %s",username_1,passwd_1);
	}
	
	if(mysql_query(mysql,src) != 0){
		write(client_socket,"success",7);
		return 1;
	}else{
		write(client_socket,"fail",4);
		return 0;
	}
}

//判断员工、家属是否注册成功
int staff_kin_register(int client_socket,MYSQL *mysql,char *str,int num){
	//将标准json格式字符串转换为json对象
	cJSON *root = cJSON_Parse(str);
	
	cJSON *username = cJSON_GetObjectItem(root,"username");
	cJSON *passwd = cJSON_GetObjectItem(root,"passwd");
	
	char *username_1 = cJSON_GetStringValue(username);
	char *passwd_1 = cJSON_GetStringValue(passwd);
	char src[1024] = {0};
	printf("%d\n",num);
	if(num == 4){
		//判断家属是否注册成功
		sprintf(src,"insert into family_members(username,passwd) values('%s','%s')",username_1,passwd_1);
	}else{
		//判断员工是否注册成功
		sprintf(src,"insert into nurse(nursename,passwd) values('%s','%s')",username_1,passwd_1);
	}
	printf("%s\n",src);
	if(mysql_query(mysql,src) == 0){
		write(client_socket,"success",7);
		return 1;
	}else{
		write(client_socket,"fail",4);
		return 0;
	}
}

//判断删除员工、家属是否成功
int delete_0(int client_socket,MYSQL *mysql,char *str,int num){
	//将标准json格式字符串转换为json对象
	cJSON *root = cJSON_Parse(str);
	
	cJSON *username = cJSON_GetObjectItem(root,"username");
	
	char *username_1 = cJSON_GetStringValue(username);
	char src[1024] = {0};
	if(num == 6){
		//判断删除员工是否成功
		sprintf(src,"delete from nurse where nursename='%s'",username_1);
	}else{
		//判断删除家属是否成功
		sprintf(src,"delete from family_members where username='%s'",username_1);
	}
	printf("%s\n",src);
	if(mysql_query(mysql,src) != 0){
		write(client_socket,"success",7);
		return 1;
	}else{
		write(client_socket,"fail",4);
		return 0;
	}
}

//判断员工、家属修改密码是否成功
int change_passwd(int client_socket,MYSQL *mysql,char *str,int num){
	//将标准json格式字符串转换为json对象
	cJSON *root = cJSON_Parse(str);
	
	cJSON *username = cJSON_GetObjectItem(root,"username");
	cJSON *passwd = cJSON_GetObjectItem(root,"passwd");
	cJSON *newpasswd = cJSON_GetObjectItem(root,"newpasswd");
	
	char *username_1 = cJSON_GetStringValue(username);
	char *passwd_1 = cJSON_GetStringValue(passwd);
	char *newpasswd_1 = cJSON_GetStringValue(newpasswd);
	char src[1024] = {0};
	if(num == 8){
		sprintf(src,"select * from nurse where nursename = %s and passwd = %s",username_1,passwd_1);
	}else{
		sprintf(src,"select * from family_members where username = %s and passwd = %s",username_1,passwd_1);
	}
	if(mysql_query(mysql,src) != 0){
		memset(src,0,1024);
		if(num == 8){
			//员工修改密码
			sprintf(src,"update nurse set passwd='%s' where nursename = '%s'",newpasswd_1,username_1);
		}else{
			//用户修改密码
			sprintf(src,"update family_members set passwd='%s' where username = '%s'",newpasswd_1,username_1);
		}
	}
	if(mysql_query(mysql,src) == 0){
		write(client_socket,"success",7);
		return 1;
	}else{
		write(client_socket,"fail",4);
		return 0;
	}
}

//判断员工请假申请是否提交成功
int leave(int client_socket,MYSQL *mysql,char *str){
	//将标准json格式字符串转换为json对象
	cJSON *root = cJSON_Parse(str);
	
	cJSON *username = cJSON_GetObjectItem(root,"username");
	cJSON *reason = cJSON_GetObjectItem(root,"reason");
	cJSON *start_time = cJSON_GetObjectItem(root,"start_time");
	cJSON *end_time = cJSON_GetObjectItem(root,"end_time");
	
	char *username_1 = cJSON_GetStringValue(username);
	char *reason_1 = cJSON_GetStringValue(reason);
	char *start_time_1 = cJSON_GetStringValue(start_time);
	char *end_time_1 = cJSON_GetStringValue(end_time);
	
	char src[1024] = {0};
	sprintf(src,"insert into leaves(nursename,reason,start_time,end_time) values('%s','%s','%s','%s')",\
	username_1,reason_1,start_time_1,end_time_1);
	printf("%s\n",src);
	if(mysql_query(mysql,src) == 0){
		write(client_socket,"success",7);
		return 1;
	}else{
		write(client_socket,"fail",4);
		return 0;
	}
}

//查看请假信息
int cat_leave(int client_socket,MYSQL *mysql,char *str){
	// 创建 JSON 对象
    cJSON* root = cJSON_CreateObject();
	char sql[1000];
	//行数
	my_ulonglong Row = 0;
	//创建结果集
	MYSQL_RES *res;
	//创建获取一行数据
	MYSQL_ROW row;
	sprintf(sql,"select * from leaves");
	mysql_query(mysql,sql);
	res = mysql_store_result(mysql);
	Row = mysql_num_rows(res);
	//创建数组型的json对象
	cJSON *username = cJSON_CreateArray();
	cJSON *reason = cJSON_CreateArray();
	cJSON *start_time = cJSON_CreateArray();
	cJSON *end_time = cJSON_CreateArray();
	cJSON *examine = cJSON_CreateArray();
	
	cJSON *username_0;
	cJSON *reason_0;
	cJSON *start_time_0;
	cJSON *end_time_0;
	cJSON *examine_0;
	
	int i = 0,j = 0;
	for(i = 0;i < Row;i++){
		j = 0;
		row = mysql_fetch_row(res);
		username_0 = cJSON_CreateString(row[j++]);
		reason_0 = cJSON_CreateString(row[j++]);
		start_time_0 = cJSON_CreateString(row[j++]);
		end_time_0 = cJSON_CreateString(row[j++]);
		examine_0 = cJSON_CreateString(row[j]);
		//向数组里添加数据
		cJSON_AddItemToArray(username,username_0);
		cJSON_AddItemToArray(reason,reason_0);
		cJSON_AddItemToArray(start_time,start_time_0);
		cJSON_AddItemToArray(end_time,end_time_0);
		cJSON_AddItemToArray(examine,examine_0);
	}
	cJSON_AddItemToObject(root,"username",username);
	cJSON_AddItemToObject(root,"reason",reason);
	cJSON_AddItemToObject(root,"start_time",start_time);
	cJSON_AddItemToObject(root,"end_time",end_time);
	cJSON_AddItemToObject(root,"examine",examine);
	
	char src[1024] = {0};
	strcpy(src,cJSON_Print(root));
	printf("%s\n",src);
	write(client_socket,src,strlen(src));
	return 0;
}

//审批假条
int gly_examine(int client_socket,MYSQL *mysql,char *str){
	//将标准json格式字符串转换为json对象
	cJSON* root = cJSON_Parse(str);
	
	cJSON *username = cJSON_GetObjectItem(root,"username");
	cJSON *examine = cJSON_GetObjectItem(root,"examine");
	
	char *username_0 = cJSON_GetStringValue(username);
	char *examine_0 = cJSON_GetStringValue(examine);
	
	char src[1024] = {0};
	sprintf(src,"update leaves set examine='%s' where nursename = '%s'",examine_0,username_0);
	printf("%s\n",src);
	if(mysql_query(mysql,src) == 0){
		write(client_socket,"success",7);
		return 1;
	}else{
		write(client_socket,"fail",4);
		return 0;
	}
}

//判断档案是否入库成功
int write_files(int client_socket,MYSQL *mysql,char *str){
	//将标准json格式字符串转换为json对象
	cJSON* root = cJSON_Parse(str);
	
	cJSON *name = cJSON_GetObjectItem(root,"name");
	cJSON *age = cJSON_GetObjectItem(root,"age");
	cJSON *sex = cJSON_GetObjectItem(root,"sex");
	cJSON *id_number = cJSON_GetObjectItem(root,"id_number");
	cJSON *phone = cJSON_GetObjectItem(root,"phone");
	cJSON *family_members = cJSON_GetObjectItem(root,"family_members");
	cJSON *health_status = cJSON_GetObjectItem(root,"health_status");
	cJSON *pervalence = cJSON_GetObjectItem(root,"pervalence");
	cJSON *medicine_fee = cJSON_GetObjectItem(root,"medicine_fee");
	cJSON *month_fee = cJSON_GetObjectItem(root,"month_fee");
	
	char *name1 = cJSON_GetStringValue(name);
	ssize_t age1 = cJSON_GetNumberValue(age);
	char *sex1 = cJSON_GetStringValue(sex);
	char *id_number1 = cJSON_GetStringValue(id_number);
	char *phone1 = cJSON_GetStringValue(phone);
	char *family_members1 = cJSON_GetStringValue(family_members);
	char *health_status1 = cJSON_GetStringValue(health_status);
	char *pervalence1 = cJSON_GetStringValue(pervalence);
	ssize_t medicine_fee1 = cJSON_GetNumberValue(medicine_fee);
	ssize_t month_fee1 = cJSON_GetNumberValue(month_fee);
	
	char src[1024] = {0};
	//printf("name:%s-age:%ld-sex:%s-id_number:%s-phone:%s-family_members:%s\
	-health_status:%s-pervalence:%s-medicine_feel:%ld-month_fee:%ld\n",\
	name1,age1,sex1,id_number1,phone1,family_members1,health_status1,pervalence1,medicine_fee1,month_fee1);
	sprintf(src,"insert into old_files (name,age,sex,id_number,family_members,phone,health_status,\
	pervalence,medicine_fee,month_fee) values('%s',%ld,'%s','%s','%s','%s','%s','%s',%ld,%ld)",\
	name1,age1,sex1,id_number1,phone1,family_members1,health_status1,pervalence1,medicine_fee1,month_fee1);
	printf("%s\n",src);
	if(mysql_query(mysql,src) == 0){
		write(client_socket,"success",7);
		return 1;
	}else{
		write(client_socket,"fail",4);
		return 0;
	}
}

























